<?php

/**
 * Class for viewing a CSS selector.
 *
 * @author Ian Clarence ian.clarence@gmail.com
 */ 
class ViewSelector extends Component {

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {        
        parent::__construct($fields);
    }

    /**
     * Setting the tag value in the selector.
     *
     * @param string $tag
     */
    public function setTag($tag) {
        $this->fields['tag'] = $tag;
    }

    /**
     * Removing the tag value from the selector.
     */
    public function unsetTag() {
        unset($this->fields['tag']);
    }

    /**
     * Setting the id value in the selector.
     *
     * @param string $id
     */
    public function setElementId($id) {
        $this->fields['element_id'] = $id;
    }

    /**
     * Removing the id value from the selector.
     */
    public function unsetElementId() {
        unset($this->fields['element_id']);
    }

    /**
     * Determining whether there is a class named '$className'.
     *
     * @param string $className
     * @return bool
     */
    private function classSet($className) {
        foreach($this->fields['class_array'] as $value) {
            if ($value == $className) {
                return TRUE;
            }
        }
        return FALSE;
    }

    /**
     * Adding a class '$className'.
     *
     * @param string $className
     */
    public function addClass($className) {
        if (!isset($this->fields['class_array'])) {
            $this->fields['class_array'] = array();
        }
        $this->fields['class_array'][$className] = $className;
    }

    /**
     * Removing the class '$className'.
     *
     * @param string $className
     */
    public function removeClass($className) {
        unset($this->fields['class_array'][$className]);
    }

    /**
     * Removing all the classes.
     */
    public function resetClassArray() {
        $this->fields['class_array'] = array();
    }

    /**
     * Setting the pseudo-class value in the selector.
     *
     * @param string $pseudoClass
     */
    public function setPseudoClass($pseudoClass) {
        $this->fields['pseudo_class'] = $pseudoClass;
    }

    /**
     * Removing the pseudo-class value from the selector.
     */
    public function unsetPseudoClass() {
        unset($this->fields['pseudo_class']);
    }

    /**
     * Returns a simple CSS selector.
     *
     * @param string $tag
     * @param string $id
     * @param string $className
     * @param string $pseudoClass
     * @return ViewSelector
     */
    public static function selectorInit(
        $tag = NULL, 
        $id = NULL, 
        $className = NULL, 
        $pseudoClass = NULL
    ) {
        $selector = new self(array());
        if ($tag != NULL) {
            $selector->setTag($tag);
        }
        if ($id != NULL) {
            $selector->setElementId($id);
        }
        if ($className != NULL) {
            $selector->addClass($className);
        }
        if ($pseudoClass != NULL) {
            $selector->setPseudoClass($pseudoClass);
        }
        return $selector;
    }
    
    /**
     * Viewing the formatted selector.
     *
     * @return string
     */
    public function view() {
        $output = '';
        
        /**
         * Tag. 
         */
        if (isset($this->fields['tag'])) {
            $output .= $this->fields['tag'];
        }
        
        /**
         * Id. 
         */
        if (isset($this->fields['element_id'])) {
            $output .= '#' . $this->fields['element_id'];
        }

        /**
         * Class array.
         */
        if (isset($this->fields['class_array'])) {
            if (count($this->fields['class_array']) > 0) {
                $output .= '.' . implode('.', $this->fields['class_array']);
            }
        }
        
        /**
         * Pseudo-class. 
         */
        if (isset($this->fields['pseudo_class'])) {
            $output .= ':' . $this->fields['pseudo_class'];
        }
        
        return $output;
    }
    
}